package com.xingkeduo.websocket.hanler;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;

import static com.xingkeduo.util.ScreenMapUtil.LOGIN_SESSION_MAP;

/**
 * @author chi  2019-05-24 17:00
 **/
@Component
@ServerEndpoint("/websocket/login/{identificationCode}")
@Slf4j
public class LoginHandler {


    @OnOpen
    public void onOpen(@PathParam("identificationCode") String identificationCode, Session session) {
        log.info("登陆连接:{}",identificationCode);
        LOGIN_SESSION_MAP.put(identificationCode, session);
    }

    @OnError
    public void onError(@PathParam("identificationCode") String identificationCode,Session session, Throwable error) {
        log.error("{}登陆连接服务端发生了错误:{}", identificationCode, error);
    }

    /**
     * 连接关闭
     */
    @OnClose
    public void onClose(@PathParam("identificationCode") String identificationCode, Session session) {
        log.error("登陆关闭:{}", identificationCode);
        LOGIN_SESSION_MAP.remove(identificationCode);
    }

    /**
     * 收到客户端的消息
     *
     * @param message 消息
     * @param session 会话
     */
    @OnMessage
    public void onMessage(@PathParam("identificationCode") String identificationCode, String message, Session session) {
        log.info("来自客户端消息：" + message + "客户端的id是：" + session.getId());
    }

}
